﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;


public partial class includes_uclrLogin : System.Web.UI.UserControl
{
    Manager manager = new Manager(true, "ConnectionString");
    Manager mgr = new Manager(true, "ConnectionString");
    protected bool isFeatured = false;
    protected string pageName;
    protected string pageHeading;
    protected localFunction obj = new localFunction();

  
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["ShowLayer"] = "";
        //btnSubmit.ImageUrl = Session["strHttps"].ToString() + "images/btn-submit-now.gif";

        pageName = Request.Url.AbsolutePath.Substring(Request.Url.AbsolutePath.LastIndexOf("/"), (Request.Url.AbsolutePath.Length - Request.Url.AbsolutePath.LastIndexOf("/")));
        if (pageName.ToUpper() == "/SearchDoctors_adv.aspx".ToUpper())
        {
            pageName = "img_search-doc.jpg";
            pageHeading = "Search Doctors";
            //PageImg.AlternateText = "Health & Medical Doctor Community";
            //PageImg.ImageUrl = Session["strHttp"] + "images/img_search-doc.jpg";
        }
        if (pageName.ToUpper() == "/SearchDentist_adv.aspx".ToUpper())
        {
            pageName = "img_search-doc.jpg";
            pageHeading = "Search dentist";
            //PageImg.AlternateText = "Health & Medical Doctor Community";
            //PageImg.ImageUrl = Session["strHttp"] + "images/img_search-doc.jpg";
        }
        if (pageName.ToUpper() == "/topDoctor.aspx".ToUpper())
        {
            pageName = "img_top-doc.jpg";
            pageHeading = "Top Doctors";
            //PageImg.AlternateText = "Health & Medical Doctor Community";
            //PageImg.ImageUrl = Session["strHttp"] + "images/img_top-doc.jpg";
        }
        if (pageName.ToUpper() == "/community.aspx".ToUpper())
        {
            pageName = "img_community.jpg";
            //pageHeading = "Welcome to our community"; //Commented by Wahaj Khan June 01 2011
            pageHeading = "Welcome to Doc-Friend";  //Added by Wahaj Khan June 01 2011
            //PageImg.AlternateText = "Health & Medical Doctor Community";
            //PageImg.ImageUrl = Session["strHttp"] + "images/img_community.jpg";
        }
        if (pageName.ToUpper() == "/doctorRegistration.aspx".ToUpper())
        {
            pageName = "img_doc-registration.jpg";
            pageHeading = "Doctor Registration";
            //PageImg.AlternateText = "Health & Medical Doctor Community";
            //PageImg.ImageUrl = Session["strHttps"] + "images/img_doc-registration.jpg";
        }
        if (pageName.ToUpper() == "/dentistRegistration.aspx".ToUpper())
        {
            pageName = "img_dentist-registration.jpg";
            pageHeading = "Dentist Registration";
            //PageImg.AlternateText = "Health & Medical Doctor Community";
            //PageImg.ImageUrl = Session["strHttps"] + "images/img_dentist-registration.jpg";
        }
        if (pageName.ToUpper() == "/patient_registration.aspx".ToUpper())
        {
            pageName = "img_patient-registration.jpg";
            pageHeading = "Patient Registration";
            //PageImg.AlternateText = "Health & Medical Doctor Community";
            //PageImg.ImageUrl = Session["strHttps"] + "images/img_patient-registration.jpg";
        }

        if (pageName.ToUpper() == "/FeaturedDoctor.aspx".ToUpper())
        {
            pageName = "img_featured-doc.jpg";
            pageHeading = "Featured Doctors";
            //PageImg.AlternateText = "Health & Medical Dentist Community";
            //PageImg.ImageUrl = Session["strHttp"] + "images/img_featured-doc.jpg";
        }

        if (pageName.ToUpper() == "/FeaturedDentist.aspx".ToUpper())
        {
            //pageName = "img_featured-dentist.jpg";
            //pageHeading = "Featured Dentists";
            ////PageImg.AlternateText = "Health & Medical Dentist Community";
            ////PageImg.ImageUrl = Session["strHttp"] + "images/img_featured-dentist.jpg";    
            pageName = "img_search-doc.jpg";
            pageHeading = "Featured Dentists";
            //PageImg.AlternateText = "Health & Medical Dentist Community";
            //PageImg.ImageUrl = Session["strHttp"] + "images/img_search-doc.jpg";
        }


        //ADD AT 16/02/2008 BY RANJEET
        if (!Page.IsPostBack)
        {
            //Fill Doctor Name and his/her speciality in your account box 
            if (Session["DoctorType"].ToString().ToUpper() == "Doctor".ToUpper())
            {
                //lnkFacePage.Text = "Doctor Face Page";
                ShowDrInformation();

            }
            else if (Session["DoctorType"].ToString().ToUpper() == "Dentist".ToUpper())
            {
                //lnkFacePage.Text = "Dentist Face Page";
                ShowDntInformation();
            }
        }
        btnSubmit.Attributes.Add("onclick", "return checkLogin('" + txtDocEmail.ClientID + "','" + txtDocPass.ClientID + "');");
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        #region Clear old values once someone tried to login
        lblError.Text = "";

        Session["userID"] = "";
        Session["userType"] = "";
        Session["LoginName"] = "";
        Session["UserEmail"] = "";
        Session["DoctorType"] = "";
        Session["DentistType"] = "";
        Session["DentistDoctorEmail"] = "";//Added for Face Page
        #endregion

        localFunction lfunc = new localFunction();
        if (chkremberdoc.Checked)
        {
            lfunc.rememberMe(localFunction.DOCTORCOOKIE, txtDocEmail.Text, txtDocPass.Text);
        }
        else
            lfunc.RemoveCookie(localFunction.DOCTORCOOKIE);

        //  17-11-2007
        //  CODE MODIFIED TO LET DOCTOR, DENTIST & PATIENT LOGIN FROM SINGLE LOGIN BOX
        //  CODE STARTS

        if (string.Compare(rbtnlDoctorType.SelectedValue, "Doctor", true) == 0) // If Doctor is being logged in
        {
            #region Check Docotor Login

            if (chkremberdoc.Checked)
                lfunc.Cookie(localFunction.DOCTORCOOKIE, txtDocEmail.Text.Trim(), txtDocPass.Text.Trim(), true);
            else
                lfunc.Cookie(localFunction.DOCTORCOOKIE, txtDocEmail.Text.Trim(), txtDocPass.Text.Trim(), false);

            Doctor doc = new Doctor();
            lblError.Text = doc.DoLogin(txtDocEmail.Text, txtDocPass.Text, "doctor/");
            lblError.Visible = true;
            #endregion
        }
        else if (string.Compare(rbtnlDoctorType.SelectedValue, "Dentist", true) == 0) // If Dentist is being logged in
        {
            #region Check Dentist Login

            if (chkremberdoc.Checked)
                lfunc.Cookie(localFunction.DENTISTCOOKIE, txtDocEmail.Text.Trim(), txtDocPass.Text.Trim(), true);
            else
                lfunc.Cookie(localFunction.DENTISTCOOKIE, txtDocEmail.Text.Trim(), txtDocPass.Text.Trim(), false);

            // CODE CHANGED TO USE CLASS FUNCTIONS FOR DENTIST LOGIN
            // DATE 18TH FEB 2008

            clsDentist dent = new clsDentist();
            lblError.Text = dent.DoDentistLogin(txtDocEmail.Text, txtDocPass.Text, "dentist/");
            lblError.Visible = true;

            #endregion
        }
        else
        {
            // PATIENT LOGIN CHECK - INCORPORATED FROM PHASE IV ON 17-11-2007 - DEEPAK

            if (chkremberdoc.Checked)
                lfunc.Cookie(localFunction.PATIENTCOOKIE, txtDocEmail.Text.Trim(), txtDocPass.Text.Trim(), true);
            else
                lfunc.Cookie(localFunction.PATIENTCOOKIE, txtDocEmail.Text.Trim(), txtDocPass.Text.Trim(), false);

            // CODE CHANGED TO USE CLASS FUNCTIONS FOR DENTIST LOGIN
            // DATE 18TH FEB 2008

            Patient SiteUser = new Patient();
            lblError.Text = SiteUser.DoPatientLogin(txtDocEmail.Text, txtDocPass.Text, "user/");
            lblError.Visible = true;

          
        }

        #region If any user is logged in then show Logout Link
        if (!Equals(Session["userID"], null) && Session["userID"].ToString().Length > 0)
        {
            //   ((LinkButton)Master.FindControl("lnk_log")).Text = "Logout";
        }
        #endregion

       
    }

    protected void lnkbtnRegistration_Click(object sender, EventArgs e)
    {
        if (string.Compare(rbtnlDoctorType.SelectedValue, "Doctor", true) == 0) // If Doctor - Access Profile
        {
            Response.Redirect(Session["strHttps"].ToString() + "doctor/doctorRegistration.aspx");
        }
        else if (string.Compare(rbtnlDoctorType.SelectedValue, "Dentist", true) == 0)
        {
            Response.Redirect(Session["strHttps"].ToString() + "Dentist/DentistRegistration.aspx");
        }
        else
        {
            Response.Redirect(Session["strHttps"].ToString() + "patient/patient_registration.aspx");
        }
    }

    protected void lnkbtnForgetPass_Click(object sender, EventArgs e)
    {
        if (string.Compare(rbtnlDoctorType.SelectedValue, "Doctor", true) == 0) // If Doctor - Access Profile
        {
            Response.Redirect(Session["strHttp"].ToString() + "doctor/doctorForgotPass.aspx");
        }
        else if (string.Compare(rbtnlDoctorType.SelectedValue, "Doctor", true) == 0)
        {
            Response.Redirect(Session["strHttp"].ToString() + "Dentist/DentistForgotPass.aspx");
        }
        else
        {
            Response.Redirect(Session["strHttp"].ToString() + "patient/patient_password.aspx");
        }

    }

    protected void lnkbtnAccessProfile_Click(object sender, EventArgs e)
    {
        if (string.Compare(rbtnlDoctorType.SelectedValue, "Doctor", true) == 0) // If Doctor - Access Profile
        {
            Response.Redirect(Session["strHttps"].ToString() + "doctor/AccessProfile.aspx");
        }
        else
        {
            Response.Redirect(Session["strHttps"].ToString() + "Dentist/DentistAccessProfile.aspx");
        }
    }

    //ADD CODE FOR LINK DOCTOR CONTENTS AT 16/02/2008 BY RANJEET
    protected void ShowDrInformation()
    {
        //fetch doctor information using Session["userID"] from tbl_Doctors,tbl_Speciality
        if (Session["userID"].ToString() != "")
        {
            string SQL = " SELECT  (tbl_Doctors.FName + ' ' + tbl_Doctors.MName  + ' ' + tbl_Doctors.LName) AS name,isnull(tbl_Speciality.Name,''), ";
            SQL += "isnull((select top 1 1 from tbl_bidding where trx_status=1 and dr_id=" + Session["userID"] + "),0) as isfeture ";
            SQL = SQL + " FROM ";
            SQL = SQL + " tbl_Doctors LEFT OUTER JOIN tbl_Speciality ON tbl_Doctors.SpecialityID = tbl_Speciality.Id";
            SQL = SQL + " WHERE tbl_Doctors.Id = " + Session["userID"];

            using (System.Data.SqlClient.SqlDataReader dr = mgr.ExecuteQuery(SQL))
            {
                if (dr.Read())
                {
                    switch (dr.GetInt32(2))
                    {
                        case 1:
                            isFeatured = true;
                            break;
                        default:
                            isFeatured = false;
                            break;
                    }
                    Session["DoctorName"] = dr.GetSqlValue(0).ToString(); // Added By Wahaj Khan Nov 27 2011
                }
                dr.Close();
            }
        }


    }
    protected void linkEditDrAccount_Click1(object sender, EventArgs e)
    {
        //Redirect page on doctorRegistration.aspx for edit the information of login doctor        
        Session["pageFromOrAction"] = "Edit";
        Response.Redirect(Session["strHttps"] + "doctor/doctorRegistration.aspx");
    }
    protected void Doctor_search1_Load(object sender, EventArgs e)
    {

    }
    protected void lnkViewProfile_Click(object sender, EventArgs e)
    {

        obj.GetDrInfo(false);
        // Response.Redirect("DoctorsDetails.aspx?id=" + Session["userid"].ToString());             
    }
    //END
    //ADD CODE FOR LINK DENTIST CONTENTS AT 16/02/2008 BY RANJEET
    #region ShowDrInformation method for Fill Dentist Name and his/her speciality in your account box
    protected void ShowDntInformation()
    {
        if (Session["userID"].ToString() != "")
        {
            //fetch Dentist information using Session["userID"] from tbl_Dentists,tbl_DentistSpecialty
            string SQL = " SELECT (tbl_Dentists.FName + ' ' + tbl_Dentists.MName  + ' ' + tbl_Dentists.LName)AS name,tbl_DentistSpecialty.Specialty, ";
            SQL += "isnull((select top 1 1 from tbl_dentist_bidding where trx_status=1 and Dnt_Id=" + Session["userID"] + "),0) as isfeture ";
            SQL = SQL + " FROM ";
            SQL = SQL + " tbl_Dentists LEFT OUTER JOIN tbl_DentistSpecialty ON tbl_Dentists.SpecialityID = tbl_DentistSpecialty.Id";
            SQL = SQL + " WHERE tbl_Dentists.Id = " + Session["userID"];
            //Response.Write(SQL);
            //Response.End();
            using (System.Data.SqlClient.SqlDataReader dr = mgr.ExecuteQuery(SQL))
            {
                if (dr.Read())
                {
                    switch (dr.GetInt32(2))
                    {
                        case 1:
                            isFeatured = true;
                            break;
                        default:
                            isFeatured = false;
                            break;
                    }
                    Session["DentistName"] = dr.GetSqlValue(0).ToString(); // Added By Wahaj Khan Nov 27 2011
                }
                dr.Close();
            }
        }
    }
    #endregion
    protected void linkEditDntAccount_Click1(object sender, EventArgs e)
    {
        //Redirect page on doctorRegistration.aspx for edit the information of login doctor       
        Session["pageFromOrAction"] = "Edit";
        Response.Redirect(Session["strHttps"] + "dentist/DentistRegistration.aspx?id=" + Session["userid"].ToString());
    }

    protected void lnkViewDntProfile_Click(object sender, EventArgs e)
    {
        localFunction obj = new localFunction();
        obj.GetDentInfo(false);
        // Response.Redirect("DoctorsDetails.aspx?id=" + Session["userid"].ToString());             
    }
    //END
    protected void lnkFacePage_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(Session["DentistDoctorID"]) != 0)
            Session["DentistDoctorID"] = null;
        Response.Redirect(Session["strHttp"].ToString() + "Community/FacePage.aspx");
    }
}
